package cst.neu.yuejian_31.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.Toast;

import java.util.List;

import cst.neu.yuejian_31.R;
import cst.neu.yuejian_31.adapter.PostListAdapter;
import cst.neu.yuejian_31.bean.PostBean;
import cst.neu.yuejian_31.iface.PostListListener;
import cst.neu.yuejian_31.model.PostListModel;
import cst.neu.yuejian_31.activity.UpPostActivity;

/**
 * A simple {@link Fragment} subclass.
 */
public class PostFragment extends BaseFragment {

    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private PostListAdapter postListAdapter;
    private List<PostBean> list=null;
    private View view=null;
    private Context context;
    private ImageButton addPost;

    PostListListener postListListener=new PostListListener() {
        @Override
        public void onResponse(List<PostBean> listbean) {
            list=listbean;
            postListAdapter.setList(list);
        }

        @Override
        public void onFail(String msg) {

            Toast.makeText(context,msg, Toast.LENGTH_SHORT).show();
        }
    };
    public PostFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view=inflater.inflate(R.layout.fragment_post, container, false);
        context=getActivity();
        addPost= (ImageButton) view.findViewById(R.id.imageButton_addpost);
        addPost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, UpPostActivity.class);
                startActivity(intent);
            }
        });
        PostListModel postListModel=new PostListModel();
        postListModel.getPostList(postListListener);
        init();
        return view;
    }

    private void init() {
        context=this.getActivity();
        recyclerView= (RecyclerView) view.findViewById(R.id.recyclerView_post);
        layoutManager=new GridLayoutManager(context,2);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        postListAdapter=new PostListAdapter(context);
        postListAdapter.setList(list);
        recyclerView.setAdapter(postListAdapter);
//        recyclerView.addItemDecoration(new DividerItemDecoration(context, LinearLayoutManager.HORIZONTAL));
    }

}
